home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / mpeg2decode / src / audio.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  9KB  |  325 lines

  1. /*** audio.c - MPEG Layer 1/2 audio handling in a MPEG stream ***/
  2.  
  3. /* (c) 1999 Jesper Svennevid */
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<ctype.h>
  8.  
  9. #include "config.h"
  10. #include "mpeg2dec.h"
  11. #include "audio.h"
  12. #include "global.h"
  13. #include "mpegaudio/common.h"
  14. #include "mpegaudio/decoder.h"
  15.  
  16. extern int Decode_Audio();
  17.  
  18. struct AudioBuffer
  19. {
  20.     /* contains as a max */ 
  21.  
  22.     unsigned char *databuffer;
  23.  
  24.     struct AudioBuffer *next;
  25. };
  26.  
  27. #define MAX_AUDIOBUFFER_SIZE    65536   /* size of one audio-buffer */
  28. #define MAX_AUDIOFRAMES_DECODE  16  /* number of audio-frames to decode before flushing */
  29.  
  30. struct AudioBuffer *ab_current = NULL;  /* current audio-buffer reading from */
  31. struct AudioBuffer *ab_feed = NULL; /* current audio-buffer feeding into */
  32. struct AudioBuffer *ab_free = NULL; /* free data-buffers */
  33.  
  34. /* writing offset into current buffer */
  35.  
  36. unsigned long feedOffset = NULL;
  37.  
  38. /* reading offset into current buffer */
  39.  
  40. unsigned long readOffset = NULL;
  41.  
  42. /* how much data present in the buffer */
  43.  
  44. unsigned long audioBufferSize = NULL;
  45.  
  46. void audio_feedbuffer( unsigned char *data )
  47. {
  48.     if(ab_feed)
  49.     {
  50.         if(feedOffset>=MAX_AUDIOBUFFER_SIZE)
  51.         {
  52.             if(ab_free)
  53.             {
  54.                 struct AudioBuffer *temp = ab_free;
  55.  
  56.                 ab_free = temp -> next;
  57.                 temp -> next = NULL;
  58.                 ab_feed -> next = temp;
  59.                 ab_feed = temp;
  60.             }
  61.             else
  62.             {
  63.                 struct AudioBuffer *temp = malloc(sizeof(struct AudioBuffer));
  64.                 if(temp)
  65.                 {
  66.                     if( (temp -> databuffer = malloc(MAX_AUDIOBUFFER_SIZE)) )
  67.                     {
  68.                         temp -> next = NULL;
  69.                         ab_feed -> next = temp;
  70.                         ab_feed = temp;
  71.                     }
  72.                     else
  73.                         exit(0);
  74.                 }
  75.                 else
  76.                     exit(0);
  77.             }
  78.             feedOffset = NULL;
  79.         }
  80.     }
  81.     else
  82.     {
  83.         if( (ab_feed = malloc(sizeof(struct AudioBuffer))) )
  84.         {
  85.             if( !(ab_feed -> databuffer = malloc(MAX_AUDIOBUFFER_SIZE)) )
  86.                 exit(0);
  87.         }
  88.         else
  89.             exit(0);
  90.         ab_current = ab_feed;
  91.     }
  92.  
  93.     /* feed mpeg-audio data to buffer */
  94.  
  95. #ifdef STORM
  96.     ab_feed -> databuffer[feedOffset++] = (unsigned char)data;
  97. #else
  98.     ab_feed -> databuffer[feedOffset++] = data;
  99. #endif
  100.     audioBufferSize++;
  101. }
  102.  
  103. unsigned char audio_pollbuffer()
  104. {
  105.     if(ab_current)
  106.     {
  107.         if((ab_current==ab_feed)&&(readOffset>=feedOffset))
  108.             return 0x00;
  109.         if(readOffset>=MAX_AUDIOBUFFER_SIZE)
  110.         {
  111.             struct AudioBuffer *temp = ab_current -> next;
  112.  
  113.             if(!temp) return 0x00;
  114.  
  115.             ab_current -> next = ab_free;
  116.             ab_free = ab_current;
  117.             ab_current = temp;          
  118.             readOffset = NULL;
  119.         }
  120.         if(audioBufferSize)
  121.         {
  122.             audioBufferSize--;
  123.             return ab_current -> databuffer[readOffset++];
  124.         }
  125.     }
  126.     return 0x00;
  127. }
  128.  
  129. unsigned long audioOpened = NULL;
  130.  
  131. typedef short PCM[2][3][SBLIMIT];
  132.     PCM FAR *pcm_sample;
  133.     PCM FAR *pcm_sample_full;
  134. typedef unsigned int SAM[2][3][SBLIMIT];
  135.     SAM FAR *sample;
  136.     SAM FAR *sample_full;
  137. typedef double FRA[2][3][SBLIMIT];
  138.     FRA FAR *fraction;
  139.     FRA FAR *fraction_full;
  140. typedef double VE[2][HAN_SIZE];
  141.     VE FAR *w;
  142.     VE FAR *w_full;
  143.  
  144.     layer             info;
  145.  
  146.     int               error_protection, crc_error_count, total_error_count;
  147.     unsigned int      old_crc, new_crc;
  148.     unsigned int      bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT],
  149.                       scale_index[2][3][SBLIMIT];
  150.     unsigned long     bitsPerSlot, samplesPerFrame, frameNum = 0;
  151.     unsigned long     frameBits, gotBits = 0;
  152.     char              t[50];
  153.     int topSb = 0;
  154.     int notEnoughData = NULL;
  155.  
  156. int Decode_Audio()
  157. {
  158.     int i,j,k,sync,stereo,clip;
  159.     static unsigned long sample_frames;
  160.     static Bit_stream_struc bs;
  161.     static frame_params fr_ps;
  162.     static int done;
  163.     static unsigned long bufferCounter;
  164.  
  165.     extern FILE *pipe_file;
  166.  
  167.     if(!Enable_Sound) return 0;
  168.  
  169.     if(audioBufferSize<1024)
  170.         return 0;   /* hmm, we should always have data to poll */
  171.  
  172.         if(!audioOpened)
  173.         {
  174.             pcm_sample = (PCM FAR *) mem_alloc((long) sizeof(PCM), "PCM Samp");
  175.             sample = (SAM FAR *) mem_alloc((long) sizeof(SAM), "Sample");
  176.             fraction = (FRA FAR *) mem_alloc((long) sizeof(FRA), "fraction");
  177.             w = (VE FAR *) mem_alloc((long) sizeof(VE), "w");
  178.  
  179.             done = FALSE;
  180.             bufferCounter = NULL;
  181.  
  182.             fr_ps.header = &info;
  183.             fr_ps.tab_num = -1;                /* no table loaded */
  184.             fr_ps.alloc = NULL;
  185.             for (i=0;i<HAN_SIZE;i++) for (j=0;j<2;j++) (*w)[j][i] = 0.0;
  186.  
  187.             open_bit_stream_r(&bs, "", BUFFER_SIZE);
  188.  
  189.             sample_frames = 0;
  190.  
  191.  
  192.             audioOpened = 1L;
  193.         }
  194.  
  195.     if(done)
  196.         return 0;
  197.  
  198.     //if(!notEnoughData)
  199.     //{
  200.             sync = seek_sync(&bs, SYNC_WORD, SYNC_WORD_LNGTH);
  201.         frameBits = sstell(&bs) - gotBits;
  202.            gotBits += frameBits;
  203.  
  204.            if (!sync) return 0;
  205.  
  206.         decode_info(&bs, &fr_ps);
  207.         hdr_to_frps(&fr_ps);
  208.     //}
  209.     //else
  210.     //  notEnoughData = NULL;
  211.  
  212.         stereo = fr_ps.stereo;
  213.         error_protection = info.error_protection;
  214.         crc_error_count = 0;
  215.         total_error_count = 0;
  216.  
  217.     //if(audioBufferSize<(((144*(bitrate[info.lay-1][info.bitrate_index]*1000)/((long)(s_freq[info.sampling_frequency]*1000)))+(info.padding?1:0))*2))
  218.     //{
  219.     //  printf("out of data\n");
  220.     //  notEnoughData = 1L;
  221.     //  return 0;
  222.     //}
  223.  
  224.     if(!pipe_file)
  225.     {
  226.         char buffer[256];
  227.  
  228.         sprintf(buffer,"AUDIO:B 16 C %ld F %ld",stereo,((long)(s_freq[info.sampling_frequency]*1000)));
  229.         pipe_file = fopen(buffer,"w");
  230.     
  231.     }
  232.  
  233.        if (error_protection) buffer_CRC(&bs, &old_crc);
  234.        switch (info.lay) {
  235.  
  236.           case 1: {
  237.  
  238.              bitsPerSlot = 32;        samplesPerFrame = 384;
  239.              I_decode_bitalloc(&bs,bit_alloc,&fr_ps);
  240.              I_decode_scale(&bs, bit_alloc, scale_index, &fr_ps);
  241.  
  242.              clip = 0;
  243.              for (i=0;i<SCALE_BLOCK;i++) {
  244.                 I_buffer_sample(&bs,(*sample),bit_alloc,&fr_ps);
  245.                 I_dequantize_sample(*sample,*fraction,bit_alloc,&fr_ps);
  246.                 I_denormalize_sample((*fraction),scale_index,&fr_ps);
  247.                 if(topSb>0)        /* clear channels to 0 */
  248.                    for(j=topSb; j<fr_ps.sblimit; ++j)
  249.                       for(k=0; k<stereo; ++k)
  250.                          (*fraction)[k][0][j] = 0;
  251.  
  252.                 for (j=0;j<stereo;j++) {
  253.                    clip += SubBandSynthesis (&((*fraction)[j][0][0]), j,
  254.                                              &((*pcm_sample)[j][0][0]));
  255.                 }
  256.                 if(pipe_file) out_fifo(*pcm_sample, 1, &fr_ps, done,
  257.                          pipe_file, &sample_frames);
  258.              }
  259.              return 1;
  260.           }
  261.  
  262.           case 2: {
  263.              bitsPerSlot = 8;        samplesPerFrame = 1152;
  264.              II_decode_bitalloc(&bs, bit_alloc, &fr_ps);
  265.              II_decode_scale(&bs, scfsi, bit_alloc, scale_index, &fr_ps);
  266.  
  267.              clip = 0;
  268.              for (i=0;i<SCALE_BLOCK;i++) {
  269.                 II_buffer_sample(&bs,(*sample),bit_alloc,&fr_ps);
  270.                 II_dequantize_sample((*sample),bit_alloc,(*fraction),&fr_ps);
  271.                 II_denormalize_sample((*fraction),scale_index,&fr_ps,i>>2);
  272.  
  273.                 if(topSb>0)        /* debug : clear channels to 0 */
  274.                    for(j=topSb; j<fr_ps.sblimit; ++j)
  275.                       for(k=0; k<stereo; ++k)
  276.                          (*fraction)[k][0][j] =
  277.                          (*fraction)[k][1][j] =
  278.                          (*fraction)[k][2][j] = 0;
  279.  
  280.                 for (j=0;j<3;j++) for (k=0;k<stereo;k++) {
  281.                    clip += SubBandSynthesis (&((*fraction)[k][j][0]), k,
  282.                                              &((*pcm_sample)[k][j][0]));
  283.                 }
  284.                 if(pipe_file) out_fifo(*pcm_sample, 3, &fr_ps, done, pipe_file,
  285.                          &sample_frames);
  286.              }
  287.              return 1;
  288.           }
  289.  
  290.        }
  291. }
  292.  
  293. #ifdef STORM
  294. void out_fifo(short FAR pcm_sample[2][3][SBLIMIT], int num, frame_params *fr_ps, int done, FILE *outFile, unsigned long *psampFrames)
  295. #else
  296. void out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames)
  297. short FAR pcm_sample[2][3][SBLIMIT];
  298. int num;
  299. frame_params *fr_ps;
  300. int done;
  301. FILE *outFile;
  302. unsigned long *psampFrames;
  303. #endif
  304. {
  305.     int i,j,l;
  306.     int stereo = fr_ps->stereo;
  307.     int sblimit = fr_ps->sblimit;
  308.     static short int outsamp[1600];
  309.     static long k = 0;
  310.  
  311.     for (i=0;i<num;i++) for (j=0;j<SBLIMIT;j++)
  312.     {
  313.         (*psampFrames)++;
  314.         for (l=0;l<stereo;l++)
  315.         {
  316.             if (!(k%(1600)) && k)
  317.             {
  318.                 fwrite(outsamp,2,1600,outFile);
  319.                 k = 0;
  320.             }
  321.             outsamp[k++] = pcm_sample[l][i][j];
  322.         }
  323.     }
  324. }
  325.